package com.yxx.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.yxx.annotation.OptLogger;
import com.yxx.entityModel.vo.Result.PageResult;
import com.yxx.entityModel.vo.Result.ResponseResult;
import com.yxx.entityModel.vo.query.RoleQuery;
import com.yxx.entityModel.vo.request.RoleRequest;
import com.yxx.entityModel.vo.response.RoleResponse;
import com.yxx.entityModel.vo.response.RoleStatusRequest;
import com.yxx.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.codehaus.jettison.AbstractDOMDocumentParser;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;

import static com.yxx.constant.OptTypeConstant.*;

/**
 * 角色控制层
 * @author yuxiangxun
 * @date 2025/2/5
 * @apiNote
 */
@RestController
@Api(tags = "角色模块")
public class RoleController {

    @Resource
    private RoleService roleService;

    /**
     * 查看后台角色列表
     * @param roleQuery
     * @return
     */
    @ApiOperation(value = "查看后台角色列表")
    @SaCheckPermission("system:role:list")
    @GetMapping("/admin/role/list")
    public ResponseResult<PageResult<RoleResponse>> roleList(RoleQuery roleQuery){
        return ResponseResult.success(roleService.roleList(roleQuery));
    }

    /**
     * 添加角色
     * @param roleRequest
     * @return
     */
    @OptLogger(value = ADD)
    @ApiOperation(value = "添加角色")
    @SaCheckPermission("system:role:add")
    @PostMapping("/admin/role/add")
    public ResponseResult<?> addRole(@Validated @RequestBody RoleRequest roleRequest){
        roleService.addRole(roleRequest);
        return ResponseResult.success();
    }

    /**
     * 删除角色
     * @param roleIdList
     * @return
     */
    @OptLogger(value = DELETE)
    @ApiOperation(value = "删除角色")
    @SaCheckPermission("system:role:delete")
    @DeleteMapping("/admin/role/delete")
    public ResponseResult<?> deleteRole(@RequestBody List<String> roleIdList){
        roleService.deleteRole(roleIdList);
        return ResponseResult.success();
    }

    /**
     * 修改角色
     *
     * @param roleRequest 角色信息
     * @return {@link ResponseResult<>}
     */
    @OptLogger(value = UPDATE)
    @ApiOperation(value = "修改角色")
    @SaCheckPermission("system:role:update")
    @PutMapping("/admin/role/update")
    public ResponseResult<?> updateRole(@Validated @RequestBody RoleRequest roleRequest) {
        roleService.updateRole(roleRequest);
        return ResponseResult.success();
    }

    /**
     * 修改角色状态
     *
     * @param roleStatus 角色状态
     * @return {@link ResponseResult<>}
     */
    @OptLogger(value = UPDATE)
    @ApiOperation(value = "修改角色状态")
    @SaCheckPermission(value = {"system:role:update", "system:role:status"}, mode = SaMode.OR)
    @PutMapping("/admin/role/changeStatus")
    public ResponseResult<?> updateRoleStatus(@Validated @RequestBody RoleStatusRequest roleStatus) {
        roleService.updateRoleStatus(roleStatus);
        return ResponseResult.success();
    }

    /**
     * 查看角色的菜单权限
     *
     * @param roleId 角色id
     * @return {@link List<Integer>} 角色的菜单权限
     */
    @ApiOperation(value = "查看角色的菜单权限")
    @SaCheckPermission("system:role:list")
    @GetMapping("/admin/role/menu/{roleId}")
    public ResponseResult<List<Integer>> listRoleMenuTree(@PathVariable("roleId") String roleId) {
        return ResponseResult.success(roleService.listRoleMenuTree(roleId));
    }
}
